/* =============================================================
 * Ketchup4j : Squeeze the speed out of your objects. 
 * =============================================================
 *
 * (C) Copyright 2007, by Saverio Miroddi
 *
 * Project Info:  http://tinyhorrorsql.sourceforge.net/
 *
 * This library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or (at 
 * your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
 * USA.  
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * ---------------------------
 * LRUCacheObject.java
 * ---------------------------
 * Author: Saverio Miroddi
 * 
 */
package org.itmozart.ketchup4j.other;

import java.lang.ref.SoftReference;

@SuppressWarnings("unchecked")
public class LRUCacheObject<K, V> {
	private static final int REF_STRONG = 0;
	private static final int REF_SOFT 	= 1;
	
	public K objectId;
	private Object object;

	public long lastTimeAccessed;
	public int memorySize;
	
	private int referenceType;

	public LRUCacheObject(K objectId, V object, int memorySize) {
		this.objectId = objectId;
		this.memorySize = memorySize;

		this.lastTimeAccessed = System.currentTimeMillis();
		
		this.object = object;
		this.referenceType = REF_STRONG;
	}

	/**
	 * Only updates time.
	 */
	public final void update() {
		lastTimeAccessed = System.currentTimeMillis();
	}

	/**
	 * Updates time, object and memSize.
	 */
	public final void updateStrong(V newObject, int newMemSize) {
		lastTimeAccessed = System.currentTimeMillis();

		this.object = newObject;
		this.memorySize = newMemSize;
	}

	/**
	 * Change to soft reference!
	 */
	public final void updateToSoft() {
		// don't care about access time
		
		object = new SoftReference<V>((V)object);
		referenceType = REF_SOFT;
	}
	
	/**
	 * This method must be called, if needed, after getObject.<br>
	 * Warning! There must be a strong reference to the internal object before
	 * calling this method.
	 */
	public final void updateToStrong() {
		lastTimeAccessed = System.currentTimeMillis();
		object = ((SoftReference<V>)object).get();
		referenceType = REF_STRONG;
	}
	
	/**
	 * We need to use this because an object could change while cleaner
	 * is cleaning, and if this happen cleaner can't know which type
	 * object is (in case of ICache.REF_MIXED).
	 */
	public final V getObject() {
		switch(referenceType) {
		case REF_STRONG: return (V)object;
		default: return ((SoftReference<V>)object).get();
		}
	}
}